package com.example.controller;


import com.example.Coupons.service.impl.CouponService;
import com.example.Utility.RedisUtils;
import com.example.Utility.Result;
import com.example.Utility.objIsNull;
import com.example.entity.lordOrder;
import com.example.service.*;
import com.example.user.UserIntegral.service.impl.IntegralService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Super
 */
@Slf4j
@RestController
@RequestMapping("orderForm")
public class lordOrderController {

    @Resource
    private OrderMessageSrvice orderMessageSrvice;

    @Resource
    private GoodsService goodsService;

    @Resource
    private IntegralService integralService;

    @Resource
    private CouponService couponService;

    @Autowired
    private ShoppingCarService shoppingCarService;
    // 分布式锁
    @Resource
    private RedissonClient redissonClient;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UsermessageService usermessageService;

    @Autowired
    private lordOrderService lordOrderService;

    /**
     * 创建订单
     */

    @PostMapping("createOrder")
    public Result createOrder(@RequestBody Object date) {
        lordOrder lre = lordOrderService.createOrder(date);
        if (objIsNull.isNull(lre)) {
            log.error("创建订单失败");
            throw new RuntimeException("\"创建订单失败\"");
        }
        return Result.succeed(lre);
    }

    /**
     * 获取订单详情 预支付
     */
    @GetMapping("/getOrder")
    public Result getOrder(@RequestParam("order_num") String order_num) {
        if (redisUtils.haveKey(order_num)) {
            return Result.succeed(redisUtils.getRedisObject(order_num, lordOrder.class));
        }
        return null;
    }


    /**
     * 实现下单
     *
     * @param lordOrder
     * @return Result
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/commentAdd")
    public Result commentAdd(@RequestBody lordOrder lordOrder) throws Exception {
        Boolean lse = lordOrderService.commentAdd(lordOrder);
        if (lse) {
            log.info("下单成功");
        } else {
            log.error("下单失败");
        }
        return Result.succeed(lse);
    }

    /**
     * 订单状态修改
     */
    @GetMapping("orderStatusUpdate")
    public Result orderStatusUpdate(Integer status, Long id) {
        if (null == status || null == id) {
            return Result.fail("失败");
        }
        lordOrder order = new lordOrder();
        order.setState(status);
        order.setId(id);
        if (lordOrderService.updateById(order)) {
            return Result.succeed("成功");
        }
        return Result.fail("失败");
    }
}
